package com.archgeek.rpc.proxy.custom;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author pizhihui
 * @date 2024-05-22 23:16
 */
public class CustomInvocationHandler implements MyInvocationHandler{


    private Object object;

    public CustomInvocationHandler(Object object){
        this.object = object;
    }


//    private ProductService target;
//
//    public Object getInstance(ProductService target) throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
//        this.target = target;
//        Class clazz = this.target.getClass();
//        // 参数1：被代理类的类加载器 参数2:被代理类的接口 参数3
//        // 这里的MyClassLoader先用new的方式保证编译不报错，后面会修改
//        return MyProxy.newProxyInstance(new MyClassLoader(),
//                clazz.getInterfaces()[0],
//                this);
//    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate  = simpleDateFormat.format(new Date());
        System.out.println("日期【"+currentDate + "】添加了一款产品xxxxxx");

        Object res = method.invoke(object, args);
//        Object res = method.invoke(this.target, args);

        return res;
    }
}
